'''
Created on 24/03/2009

@author: eh01
'''
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn
from yarra.service.webserver.handler.nodehttprequesthandler import \
    NodeHTTPRequestHandler
from yarra.logging import yarralogging
import threading


class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

class NodeHTTPServer(threading.Thread):
    '''
    NodeHTTPServer creates a HTTPServer in a new thread
    '''
    __server = None
    __port = None
    __logger = yarralogging.get_logger(yarralogging.WEBSERVER)
    
    def __init__(self, port):
        '''
        Constructor
        '''
        self.__port = port
        threading.Thread.__init__(self)
    
    def run(self):
        self.__server = MultiThreadedHTTPServer(('', self.__port), NodeHTTPRequestHandler)
#        self.__server = HTTPServer(('', self.__port), IMHTTPRequestHandler)
        self.__logger.info('Start Server')
        self.__server.serve_forever()
    
    def stop(self):
        self.__server.shutdown()
        self.__server.socket.close()
        self.__logger.info('Stopped Server')